import React from 'react'
import PropTypes from 'prop-types'
import {routerRedux} from 'dva/router'
import {connect} from 'dva'
import {Button, Col, Popconfirm, Row, Tabs, message} from 'antd'
import {Page} from 'components'
import queryString from 'query-string'
import List from './components/List'
import Filter from './components/Filter'
import Modal from './components/Modal'

const {TabPane} = Tabs


const Department = ({
                      location, dispatch, department, loading,
                    }) => {

  const {query, pathname} = location
  const {
    list, pagination, currentItem, activeKey, modalVisible, modalType, selectedRowKeys, empDataSource: dataSource,
  } = department

  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const handleTabChange = key => {
    dispatch({
      type: 'department/updateState',
      payload: {
        activeKey: key,
      },
    })
  }

  const modalProps = {
    item: modalType === 'create' ? {} : currentItem,
    visible: modalVisible,
    maskClosable: false,
    list,
    confirmLoading: loading.effects[`department/${modalType}`],
    title: `${modalType === 'create' ? '新增部门' : '编辑部门'}`,
    wrapClassName: 'vertical-center-modal',
    dataSource,
    handleSearch (key) {
      if (key) {
        dispatch({
          type: 'department/searchEmp',
          payload: {
            key,
          },
        })
      }
    },
    onOk (data) {
      dispatch({
        type: `department/${modalType}`,
        payload: data,
      })
        .then(() => {
          handleRefresh()
          message.success("操作成功")
        })
    },
    onCancel () {
      dispatch({
        type: 'department/hideModal',
      })
    },
  }

  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['department/query'],
    pagination: false,
    location,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },
    onDeleteItem (id) {
      dispatch({
        type: 'department/delete',
        payload: {ids: [id]},
      })
        .then(() => {
          handleRefresh({
            current: (list.length === 1 && pagination.current > 1) ? pagination.current - 1 : pagination.current,
          })
          message.success("删除成功")
        })
    },
    onEditItem (item) {
      dispatch({
        type: 'department/showModal',
        payload: {
          modalType: 'update',
          currentItem: item,
        },
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'department/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
  }

  const filterProps = {
    filter: {
      ...query,
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
    onAdd () {
      dispatch({
        type: 'department/showModal',
        payload: {
          modalType: 'create',
        },
      })
    },
  }

  const handleDeleteItems = () => {
    dispatch({
      type: 'department/multiDelete',
      payload: {
        ids: selectedRowKeys,
      },
    })
      .then(() => {
        handleRefresh({
          current: (list.length === selectedRowKeys.length && pagination.current > 1) ? pagination.current - 1 : pagination.current,
        })
        message.success("删除成功")
      })
  }

  return (
    <Page inner>
      <Tabs activeKey={activeKey} onChange={handleTabChange}>
        <TabPane tab={'组织架构表'} key={'group_list'}>
          <Filter {...filterProps}/>
          {
            selectedRowKeys.length > 0 &&
            <Row style={{marginBottom: 24, textAlign: 'right', fontSize: 13}}>
              <Col>
                {`选中了 ${selectedRowKeys.length} 条数据`}
                <Popconfirm title="确定要删除这些数据吗?" placement="left" onConfirm={handleDeleteItems}>
                  <Button type="primary" style={{marginLeft: 8}}>删除</Button>
                </Popconfirm>
              </Col>
            </Row>
          }
          <List {...listProps} />
          {modalVisible && <Modal {...modalProps} />}
        </TabPane>
        {/*<TabPane tab={'组织架构图'} key={'group_graph'}>*/}
          {/*<h1>组织架构图</h1>*/}
        {/*</TabPane>*/}
      </Tabs>
    </Page>
  )
}

Department.propTypes = {
  department: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({department, loading}) => ({department, loading}))(Department)
